package com.ruoyi.web.controller.mixuebingcheng;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dtflys.forest.annotation.Get;
import com.ruoyi.mixuebingcheng.domain.ShoppingCart;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mixuebingcheng.domain.vo.ShoppingCartVo;
import com.ruoyi.mixuebingcheng.domain.bo.ShoppingCartBo;
import com.ruoyi.mixuebingcheng.service.IShoppingCartService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mixuebingcheng/cart")
public class ShoppingCartController extends BaseController {

    private final IShoppingCartService iShoppingCartService;

    /**
     * 根据用户 id 查询购物车数据
     * @param id
     * @return
     */
    @GetMapping("/selectAllCarts")
    public R selectAllCarts(@RequestParam Long id){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, id);
        queryWrapper.eq(ShoppingCart::getStatus, 1);
        queryWrapper.gt(ShoppingCart::getNum, 0);
        return R.ok(iShoppingCartService.list(queryWrapper));
    }


    /**
     * 新增购物车数据
     * @param shoppingCart
     * @return
     */
    @PostMapping("/addCarts")
    public R<String> addCarts(@RequestBody ShoppingCart shoppingCart){
        iShoppingCartService.save(shoppingCart);
        return R.ok("新增商品成功！");
    }


    /**
     * 修改购物车数据
     * @param shoppingCart
     * @return
     */
    @PostMapping("/updateCarts")
    public R updateCarts(@RequestBody ShoppingCart shoppingCart){
        return R.ok(iShoppingCartService.updateById(shoppingCart));
    }


    /**
     * 查询【请填写功能名称】列表
     */
    @SaCheckPermission("mixuebingcheng:cart:list")
    @GetMapping("/list")
    public TableDataInfo<ShoppingCartVo> list(ShoppingCartBo bo, PageQuery pageQuery) {
        return iShoppingCartService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @SaCheckPermission("mixuebingcheng:cart:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ShoppingCartBo bo, HttpServletResponse response) {
        List<ShoppingCartVo> list = iShoppingCartService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", ShoppingCartVo.class, response);
    }

    /**
     * 获取【请填写功能名称】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mixuebingcheng:cart:query")
    @GetMapping("/{id}")
    public R<ShoppingCartVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iShoppingCartService.queryById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @SaCheckPermission("mixuebingcheng:cart:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ShoppingCartBo bo) {
        return toAjax(iShoppingCartService.insertByBo(bo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ShoppingCartBo bo) {
        return toAjax(iShoppingCartService.updateByBo(bo));
    }

    /**
     * 删除【请填写功能名称】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mixuebingcheng:cart:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iShoppingCartService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
